package com.starwish.starwishblog.web.front;


import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Tag;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.ArticleService;
import com.starwish.starwishblog.vo.ArticleVo;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("article")
@Api
public class FArticleController {

    @Autowired
    ArticleService articleService;

    @GetMapping("/getArticleById")
    public ResultResponse<ArticleVo> getArticleById(@RequestParam("id")Integer id){
        ResultResponse<ArticleVo> result = null;
        try {
            ArticleVo articleVo = articleService.findArticleById(id);
            result = new ResultResponse<>(200,"查询文章成功",articleVo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章失败");
        }
        return result;
    }

    @GetMapping("/getArticlesByAuthorId")
    public ResultResponse<PageInfo<ArticleVo>> getArticlesByAuthorId(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                                    @RequestParam(name="size",defaultValue = "5")Integer size,
                                                                     Integer authorId){
        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findArticlesByAuthorId(now,size,authorId);
            result = new ResultResponse<>(200,"根据作者id查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"根据作者id查询文章列表成功");
        }
        return result;
    }


    @GetMapping("/getArticlesByAuthorIdAndStatus")
    public ResultResponse<PageInfo<ArticleVo>> getArticlesByAuthorIdAndStatus(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                                     @RequestParam(name="size",defaultValue = "5")Integer size,
                                                                     @RequestParam("authorId") Integer authorId,Integer status){
        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findArticlesByAuthorIdAndStatus(now,size,authorId,status);
            result = new ResultResponse<>(200,"根据作者id查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"根据作者id查询文章列表成功");
        }
        return result;
    }


    @GetMapping("/getArticleTags")
    public ResultResponse<List<Tag>> getArticleTags(@RequestParam("articleId")Integer articleId){
        ResultResponse<List<Tag>> result = null;
        try {
            List<Tag> tags = this.articleService.findArticleTagsByArticleId(articleId);
            result = new ResultResponse<>(200,"查询文章标签成功",tags);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章标签异常");
        }

        return result;
    }

    @GetMapping("/recommendArticleList")
    public ResultResponse<PageInfo<ArticleVo>> recommendArticleList(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                           @RequestParam(name="size",defaultValue = "5")Integer size){
        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findRecommendArticleByCons(now,size);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章列表失败");
        }
        return result;
    }



    @GetMapping("/newArticleList")
    public ResultResponse<PageInfo<ArticleVo>> newArticleList(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                           @RequestParam(name="size",defaultValue = "5")Integer size){
        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findNewArticleByCons(now,size);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章列表失败");
        }
        return result;
    }

    @GetMapping("/hotArticleList")
    public ResultResponse<PageInfo<ArticleVo>> hotArticleList(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                              @RequestParam(name="size",defaultValue = "5")Integer size){
        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findHotArticleByCons(now,size);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章列表失败");
        }
        return result;
    }


    @GetMapping("/getHotArticleListByAuthorId")
    public ResultResponse<List<ArticleVo>> getHotArticleListByAuthorId(@RequestParam("authorId") Integer authorId){
        ResultResponse<List<ArticleVo>> result = null;
        try {
            List<ArticleVo> articlePage = articleService.findHotArticleByAuthorId(authorId);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章列表失败");
        }
        return result;
    }

    @GetMapping("/getHotArticleListByAuthorIdAndStatus")
    public ResultResponse<List<ArticleVo>> getHotArticleListByAuthorIdAndStatus(@RequestParam("authorId") Integer authorId,@Param("status")Integer status){
        ResultResponse<List<ArticleVo>> result = null;
        try {
            List<ArticleVo> articlePage = articleService.findHotArticleByAuthorIdAndStatus(authorId,status);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章列表失败");
        }
        return result;
    }

    @GetMapping("/getNewArticleListByAuthorId")
    public ResultResponse<List<ArticleVo>> getNewArticleListByAuthorId(@RequestParam("authorId") Integer authorId){
        ResultResponse<List<ArticleVo>> result = null;
        try {
            List<ArticleVo> articlePage = articleService.findNewArticleByAuthorId(authorId);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章列表失败");
        }
        return result;
    }
    @GetMapping("/getNewArticleListByAuthorIdAndStatus")
    public ResultResponse<List<ArticleVo>> getNewArticleListByAuthorIdAndStatus(@RequestParam("authorId") Integer authorId,@Param("status")Integer status){
        ResultResponse<List<ArticleVo>> result = null;
        try {
            List<ArticleVo> articlePage = articleService.findNewArticleByAuthorIdAndStatus(authorId,status);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章列表失败");
        }
        return result;
    }



    @GetMapping("/getCollectionArticleListByAuthorId")
    public ResultResponse<PageInfo<ArticleVo>> getCollectionArticleListByAuthorId(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                                                  @RequestParam(name="size",defaultValue = "5")Integer size,
                                                                                  @RequestParam(name="authorId")Integer authorId,
                                                                                  Integer type){
        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findCollectionArticleByCons(now,size,authorId,type);
            result = new ResultResponse<>(200,"查询收藏列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询收藏列表失败");
        }
        return result;
    }

    @PostMapping("/cancelCollect")
    public ResultResponse<Void> cancelCollect(@RequestParam(name="userId")Integer userId,
                                                 @RequestParam(name="articleId")Integer articleId){
        ResultResponse<Void> result = null;
        try {
            int i = articleService.cancelCollect(userId,articleId);
            if(i > 0){
                result = new ResultResponse<>(200,"取消收藏成功");
            }else{
                result = new ResultResponse<>(201,"取消收藏失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统错误");
        }
        return result;
    }

    @PostMapping("/changeCollectionType")
    public ResultResponse<Void> cancelCollect(@RequestParam(name="userId")Integer userId,
                                              @RequestParam(name="articleId")Integer articleId,
                                              @RequestParam(name="type")Integer type){
        ResultResponse<Void> result = null;
        try {
            int i = articleService.changeCollectionType(userId,articleId,type);
            if(i > 0){
                result = new ResultResponse<>(200,"修改收藏类型成功");
            }else{
                result = new ResultResponse<>(201,"修改收藏类型失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统错误");
        }
        return result;
    }

    @PostMapping("/cancelLike")
    public ResultResponse<Void> cancelLike(@RequestParam(name="userId")Integer userId,
                                              @RequestParam(name="articleId")Integer articleId){
        ResultResponse<Void> result = null;
        try {
            int i = articleService.cancelLike(userId,articleId);
            if(i > 0){
                result = new ResultResponse<>(200,"取消收藏成功");
            }else{
                result = new ResultResponse<>(201,"取消收藏失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统错误");
        }
        return result;
    }

    @GetMapping("/checkIfLike")
    public ResultResponse<Integer> checkIfLike(@RequestParam(name="userId")Integer userId,
                                          @RequestParam(name="articleId")Integer articleId){
        ResultResponse<Integer> result = null;
        try {
            int i = articleService.checkIfLike(userId,articleId);
            result = new ResultResponse<>(200,"查询是否点赞成功",i);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统错误");
        }
        return result;
    }


    @GetMapping("/checkIfCollect")
    public ResultResponse<Integer> checkIfCollect(@RequestParam(name="userId")Integer userId,
                                               @RequestParam(name="articleId")Integer articleId){
        ResultResponse<Integer> result = null;
        try {
            int i = articleService.checkIfCollect(userId,articleId);
            result = new ResultResponse<>(200,"查询是否收藏成功",i);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统错误");
        }
        return result;
    }

    @PostMapping("/addToLike")
    public ResultResponse<Void> addToLike(@RequestParam(name="userId")Integer userId,
                                              @RequestParam(name="articleId")Integer articleId){
        ResultResponse<Void> result = null;
        try {
            int i = articleService.addToLike(userId,articleId);
            if(i > 0){
                result = new ResultResponse<>(200,"点赞成功");
            }else{
                result = new ResultResponse<>(201,"点赞失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统错误");
        }
        return result;
    }

    @PostMapping("/addToCollection")
    public ResultResponse<Void> addToCollection(@RequestParam(name="userId")Integer userId,
                                          @RequestParam(name="articleId")Integer articleId){
        ResultResponse<Void> result = null;
        try {
            int i = articleService.addToCollection(userId,articleId);
            if(i > 0){
                result = new ResultResponse<>(200,"收藏成功");
            }else{
                result = new ResultResponse<>(201,"收藏失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统错误");
        }
        return result;
    }

    @GetMapping("/getCountByAuthorId")
    public ResultResponse<Integer> getCountByAuthorId(@RequestParam(name="authorId")Integer authorId) {
        ResultResponse<Integer> result = null;

        try {
            int i = articleService.findCountByAuthorId(authorId);
            result = new ResultResponse<>(200, "收藏成功", i);
        } catch (Exception exception) {
            result = new ResultResponse<>(305, "系统错误");
            exception.printStackTrace();
        }
        return result;
    }
    /**
     * 根据文章标题或者作者名搜索文章的方法
     * @param now
     * @param size
     * @param aname
     * @param uname
     * @return
     */
    @GetMapping("/articleList")
    public ResultResponse<PageInfo<ArticleVo>> getArticleList(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                           @RequestParam(name="size",defaultValue = "5")Integer size,
                                                           String aname,String uname){
        System.out.println(aname + uname);
        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findArticleByCons(now,size,aname,uname);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章列表失败");
        }
        return result;
    }

    @PostMapping("/addArticle")
    public ResultResponse<Void> addArticle(@RequestBody() ArticleVo article){
        ResultResponse<Void> result = null;
        System.out.println(article);
        try {
            int i = articleService.publishArticle(article);
            if(i >0){
                result = new ResultResponse<>(200,"发布文章成功");
            }else{
                result = new ResultResponse<>(201,"发布文章失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"系统异常");
        }
        return result;
    }

    @PostMapping("/modifyArticle")
    public ResultResponse<Void> modifyArticle(@RequestBody() ArticleVo article){
        ResultResponse<Void> result = null;
        System.out.println(article);
        try {
            int i = articleService.modifyArticle(article);
            if(i >0){
                result = new ResultResponse<>(200,"发布文章成功");
            }else{
                result = new ResultResponse<>(201,"发布文章失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统异常");
        }
        return result;
    }

    @GetMapping("/getArticlesByCateId")
    public ResultResponse<PageInfo<ArticleVo>> getArticleList(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                              @RequestParam(name="size",defaultValue = "5")Integer size,
                                                              @RequestParam("cateId") Integer cateId){

        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findArticleByCateId(now,size,cateId);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"查询文章列表失败");
        }
        return result;
    }

    @DeleteMapping("/deleteArticle/{id}")
    public ResultResponse<Void> delArticleById(@PathVariable(name="id")Integer id){
        ResultResponse<Void> result = null;
        try {
            int i = articleService.removeByKey(id);
            if(i > 0){
                result = new ResultResponse<>(200,"删除成功");
            }else{
                result = new ResultResponse<>(201,"删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

    @PostMapping("/addViewCountAndViewHistory")
    public ResultResponse<Void> addViewCountAndViewHistory(@RequestParam(name="userId")Integer userId,
                                                @RequestParam(name="articleId")Integer articleId){
        ResultResponse<Void> result = null;
        try {
            articleService.addViewCountAndViewHistory(userId,articleId);
            result = new ResultResponse<>(200,"添加浏览记录成功");

        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统错误");
        }
        return result;
    }



}
